class FlagExitThread extends Thread {
    private volatile boolean running = true;

    @Override
    public void run() {
        while(running){
            try{
                // 模拟工作负载
                sleep(100);
            }catch(InterruptedException e){
                System.out.println("flagExitThread interrupted...");
                break;
            }
        }
    }

    public void shutdown(){
        this.running=false;
        interrupt();  // 唤醒等待状态中的线程以便其可以检测到running变量的变化而结束循环.
    }
}

// 使用示例
FlagExitThread flagExitThread = new FlagExitThread(FlagExitThread.class.getSimpleName());